package com.yingsheng.bi.hks.repository.dao.service;

import com.yingsheng.bi.hks.dto.servicedetail.ServiceDetailQryReq;
import com.yingsheng.bi.hks.repository.dao.mapper.ServiceDetailMapper;
import com.yingsheng.bi.hks.repository.dao.po.ServiceDetail;
import com.yingsheng.bi.hks.repository.dao.po.ServiceDetailExample;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

/**
 * ServiceDetail表的数据库操作服务
 * 要求说明：
 * 1.一般情况下,要求参数不超过5个时方法入参逐个声明
 * 2.一般情况下,要求条件不超过3个时要在方法名上体现
 * 3.最好对方法入参做一定校验,尤其是对条件参数至少要做非空校验
 *
 * @author 83686
 */
@Repository
public class ServiceDetailDaoService implements DaoService {

    @Autowired
    private ServiceDetailMapper serviceDetailMapper;

    public boolean insert(ServiceDetail serviceDetail) {
        int result = serviceDetailMapper.insert(serviceDetail);
        return result == 1;
    }

    public boolean updateById(@NotBlank(message = "服务详情标识不能为空") String id) {
        return false;
    }

    public boolean updateById(ServiceDetail serviceDetail) {
        serviceDetail.setUpdateTime(new Date());
        int result = serviceDetailMapper.updateByPrimaryKeySelective(serviceDetail);
        return result == 1;
    }

    public List<ServiceDetail> query(ServiceDetailQryReq req) {
        ServiceDetailExample example = new ServiceDetailExample();
        ServiceDetailExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isNullOrEmpty(req.getId())) {
            criteria.andIdEqualTo(req.getId());
        }
        if (!StringUtil.isNullOrEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        return serviceDetailMapper.selectByExample(example);
    }

    public boolean del(String id) {
        return serviceDetailMapper.deleteByPrimaryKey(id) == 1;
    }
}
